﻿namespace ap2.Phone.Core.Services
{
    using System;
    using Microsoft.Phone.Controls;
    using Microsoft.Practices.Prism.Events;
    using System.Windows.Navigation;
    using Phone.Core.Events;
    using System.Collections.Generic;

    public class ApplicationFrameNavigationService : INavigationService
    {
        private readonly PhoneApplicationFrame frame;
        private readonly IEventAggregator eventAggregator;

        public ApplicationFrameNavigationService(PhoneApplicationFrame frame, IEventAggregator eventAggregator)
        {
            this.frame = frame;
            this.eventAggregator = eventAggregator;
            this.frame.Navigated +=new NavigatedEventHandler(frame_Navigated);
        }

        void frame_Navigated(object sender, NavigationEventArgs e)
        {
        }

        public bool CanGoBack
        {
            get { return this.frame.CanGoBack; }
        }

        public Uri CurrentSource
        {
            get { return this.frame.CurrentSource; }
        }

        public bool Navigate(Uri source, params KeyValuePair<string,object>[] parameters)
        {
            NavigationArgs args = new NavigationArgs();
            args.To = source;
            args.Parameters = parameters;
            this.eventAggregator.GetEvent<NavigatingEvent>().Publish(args);
            return this.frame.Navigate(source);
        }

        public void GoBack()
        {
            this.frame.GoBack();
        }
    }
}
